home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Amiga / dostat.c < prev    next >
C/C++ Source or Header  |  1994-09-30  |  9KB  |  320 lines

  1. RCS_ID_C="$Id: dostat.c,v 4.2 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      dostat.c - *stat() function common part 
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/utility.h>
  12. #include <libraries/usergroup.h>
  13.  
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20.  
  21. /* DOS 3.0 and MuFS extensions to file info block */
  22. #include "fibex.h"
  23.  
  24. /*
  25.  * Conversion table from Amiga filetypes to Unix filetypes
  26.  */
  27. const static mode_t ftype[ST_LINKDIR - ST_PIPEFILE + 1] = {
  28.   S_IFIFO,
  29.   S_IFREG,
  30.   S_IFREG,
  31.   S_IFREG,
  32.   S_IFREG,
  33.   S_IFREG,
  34.   S_IFDIR,
  35.   S_IFDIR,
  36.   S_IFLNK,
  37.   S_IFDIR,
  38. };
  39.  
  40. /*
  41.  * Conversion table from Amiga protections to Unix protections
  42.  * rwed -> rwx
  43.  */
  44. const static UBYTE fbits[16] =
  45. {
  46.   00, 02, 01, 03, 02, 02, 03, 03,
  47.   04, 06, 05, 07, 06, 06, 07, 07,
  48. };
  49.  
  50. void __dostat(struct FileInfoBlock *fib,
  51.           struct stat *st)
  52. {
  53.   ULONG pbits = fib->fib_Protection ^ 0xf;
  54.   short fibtype = fib->fib_DirEntryType - ST_PIPEFILE;
  55.   mode_t mode;
  56.  
  57.   if (fibtype < 0)
  58.     fibtype = 0;
  59.   else if (fibtype > ST_LINKDIR - ST_PIPEFILE)
  60.     fibtype = ST_LINKDIR - ST_PIPEFILE;
  61.  
  62.   bzero(st, sizeof(*st));
  63.  
  64.   mode = ftype[fibtype] | (fbits[pbits & 0xf] << 6)
  65.     | (fbits[(pbits >> FIBB_GRP_DELETE) & 0xf] << 3)
  66.       | fbits[(pbits >> FIBB_OTR_DELETE) & 0xf];
  67.  
  68.   if ((pbits & FIBF_PURE) != 0)
  69.     mode |= S_ISVTX;
  70.   if ((pbits & FIBF_SUID) != 0)
  71.     mode |= S_ISUID;
  72.   if ((pbits & FIBF_SGID) != 0)
  73.     mode |= S_ISGID;
  74.  
  75.   st->st_ino = fib->fib_DiskKey;
  76.   st->st_mode = mode;
  77.   st->st_nlink = 1;
  78.   st->st_uid = MU2UG(fib->fib_OwnerUID);
  79.   st->st_gid = MU2UG(fib->fib_OwnerGID);
  80.   st->st_rdev = 0;
  81.   st->st_size = fib->fib_Size;
  82.  
  83.   /* 
  84.    * Calculatory time since Jan 1 1970, UCT 
  85.    * (in reality there are an odd number of leap seconds, 
  86.    * which are not included)
  87.    */
  88.   st->st_atime = st->st_ctime = st->st_mtime =
  89.     60 * ((fib->fib_Date.ds_Days + (8*365+2)) * 24 * 60
  90.       + fib->fib_Date.ds_Minute)
  91.       + fib->fib_Date.ds_Tick / 50;
  92.  
  93.   st->st_blksize = 512;
  94.   st->st_blocks = fib->fib_NumBlocks;
  95.   st->st_dosmode = fib->fib_Protection;
  96.   st->st_type = fib->fib_DirEntryType;
  97.   st->st_comment = fib->fib_Comment;
  98. }
  99.  
  100. /****** net.lib/stat *********************************************************
  101.  
  102.     NAME
  103.         stat, lstat, fstat - get file status
  104.  
  105.     SYNOPSIS
  106.         #include <sys/types.h>
  107.         #include <sys/stat.h>
  108.  
  109.         success = stat(path, buf)
  110.  
  111.         int stat(const char *, struct stat *);
  112.  
  113.         success =  lstat(path, buf);
  114.  
  115.         int lstat(const char *, struct stat *);
  116.  
  117.         success = fstat(fd, buf);
  118.  
  119.         int fstat(int, struct stat *);
  120.  
  121.     DESCRIPTION
  122.         The stat() function obtains information about the file pointed to by
  123.         path. Read, write or execute permission of the named file is not
  124.         required, but all directories listed in the path name leading to the
  125.         file must be seachable.
  126.  
  127.         Lstat() is like stat() except in the case where the named file is a
  128.         symbolic link, in which case lstat() returns information about the
  129.         link, while stat() returns information about the file the link
  130.         references.
  131.  
  132.         The fstat() obtains the same information about an open file known by
  133.         the file descriptor fd, such as would be obtained by an open call.
  134.  
  135.         Buf is a pointer to a stat() structure as defined by <sys/stat.h>
  136.         (shown below) and into which information is placed concerning the
  137.         file.
  138.  
  139.            struct  stat
  140.            {
  141.              dev_t   st_dev;         \* unique device id *\ 
  142.              ino_t   st_ino;         \* inode of file (key block) *\ 
  143.              mode_t  st_mode;        \* Unix style mode *\ 
  144.              ushort  st_nlink;       \* number of links (unimplemented) *\ 
  145.              uid_t   st_uid;         \* owner's user ID *\ 
  146.              gid_t   st_gid;         \* owner's group ID *\ 
  147.              dev_t   st_rdev;        \* special file ID (unimplemented) *\ 
  148.              off_t   st_size;        \* file size *\ 
  149.              time_t  st_atime;       \* Time of last access *\ 
  150.              time_t  st_mtime;       \* Last modification time *\ 
  151.              time_t  st_ctime;       \* Last file status change time *\ 
  152.              long    st_blksize;     \* Size of disk block *\ 
  153.              long    st_blocks;      \* Size in blocks *\ 
  154.              long    st_dosmode;     \* DOS protection bits *\ 
  155.              short   st_type;        \* DOS file type *\ 
  156.              char   *st_comment;     \* DOS file comment *\ 
  157.            };
  158.  
  159.         The time-related fields of struct stat have same contents, time when
  160.         file data last modified.
  161.  
  162.         The status information word st_mode has bits as follows:
  163.  
  164.           #define S_ISUID  0004000    \* set user id on execution *\ 
  165.       #define S_ISGID  0002000    \* set group id on execution *\ 
  166.       #define S_ISVTX  0001000    \* save swapped text even after use *\ 
  167.       #define S_IRUSR  0000400    \* read permission for owner *\ 
  168.       #define S_IWUSR  0000200    \* write permission for owner *\ 
  169.       #define S_IXUSR  0000100    \* execute permission for owner *\ 
  170.       #define S_IRGRP  0000040    \* read permission for group *\ 
  171.       #define S_IWGRP  0000020    \* write permission for group *\ 
  172.       #define S_IXGRP  0000010    \* execute permission for group *\ 
  173.       #define S_IROTH  0000004    \* read permission for other *\ 
  174.       #define S_IWOTH  0000002    \* write permission for other *\ 
  175.       #define S_IXOTH  0000001    \* execute permission for other *\ 
  176.       #define S_IFCHR  0020000    \* character special *\ 
  177.       #define S_IFDIR  0040000    \* directory *\ 
  178.       #define S_IFBLK  0060000    \* block special *\ 
  179.       #define S_IFREG  0100000    \* regular *\ 
  180.       #define S_IFLNK  0120000    \* symbolic link *\ 
  181.       #define S_IFSOCK 0140000    \* socket *\ 
  182.       #define S_IFIFO  0010000    \* named pipe (fifo) *\ 
  183.  
  184.         For a list of access modes, see <sys/stat.h>, access(2) and chmod(2).
  185.  
  186.     RETURN VALUES
  187.         Upon successful completion a value of 0 is returned.  Otherwise, a
  188.         value of -1 is returned and errno is set to indicate the error.
  189.  
  190.     ERRORS
  191.         The functions stat() and lstat() will fail if:
  192.  
  193.         [ENOTDIR]       A component of the path prefix is not a directory.
  194.  
  195.         [ENAMETOOLONG]  A component of a pathname exceeded 255 characters,
  196.                         or an entire path name exceeded 1023 characters.
  197.  
  198.         [ENOENT]        The named file does not exist.
  199.  
  200.         [ELOOP]         Too many symbolic links were encountered in
  201.                         translating the pathname.
  202.  
  203.         [EACCES]        Search permission is denied for a component of the
  204.                         path prefix.
  205.  
  206.         [EFAULT]        Buf or name points to an invalid address.
  207.  
  208.         [EIO]           An I/O error occurred while reading from or writing
  209.                         to the file system.
  210.  
  211.         The function fstat() will fail if:
  212.  
  213.         [EBADF]   fd is not a valid open file descriptor.
  214.  
  215.         [EFAULT]  Buf points to an invalid address.
  216.  
  217.         [EIO]     An I/O error occurred while reading from or writing to the
  218.                   file system.
  219.  
  220.     SEE ALSO
  221.         chmod(),  chown()
  222.  
  223.     BUGS 
  224.         Applying fstat to a socket returns a zero'd buffer.
  225.  
  226. *******************************************************************************
  227. */
  228.  
  229. /****** net.lib/fstat *********************************************************
  230.  
  231.     SEE ALSO
  232.         stat()
  233.  
  234. *******************************************************************************
  235. */
  236.  
  237. /****** net.lib/lstat *********************************************************
  238.  
  239.     SEE ALSO
  240.         stat()
  241.  
  242. *******************************************************************************
  243. */
  244.  
  245. #ifdef DEBUGGING
  246.  
  247. #include <stdio.h>
  248. #include <fcntl.h>
  249.  
  250. void printstat(char *what, struct stat *st)
  251. {
  252.   printf("stat(%s):\n"
  253.      "  st_dev     =%lx\n"
  254.      "  st_ino     =%ld\n"
  255.      "  st_mode    =%lo\n"
  256.      "  st_nlink   =%ld\n"
  257.      "  st_uid     =%ld\n"
  258.      "  st_gid     =%ld\n"
  259.      "  st_rdev    =%lx\n"
  260.      "  st_size    =%ld\n"
  261.      "  st_atime   =%ld\n"
  262.      "  st_mtime   =%ld\n"
  263.      "  st_ctime   =%ld\n"
  264.      "  st_blksize =%ld\n"
  265.      "  st_blocks  =%ld\n",
  266.      what, 
  267.      st->st_dev,
  268.      st->st_ino,
  269.      st->st_mode,
  270.      st->st_nlink,
  271.      st->st_uid,
  272.      st->st_gid,
  273.      st->st_rdev,
  274.      st->st_size,
  275.      st->st_atime,
  276.      st->st_mtime,
  277.      st->st_ctime,
  278.      st->st_blksize,
  279.      st->st_blocks);
  280. }
  281. void main(int argc, char *argv[])
  282. {
  283.   struct stat st[1];
  284.  
  285.   if (argc > 1) {
  286.     if (stat(argv[1], st) == 0) {
  287.       printstat(argv[1], st);
  288.     } else {
  289.       perror(argv[1]);
  290.     }
  291.   } 
  292.  
  293.   argv++;
  294.   if (argc > 2) {
  295.     if (lstat(argv[1], st) == 0) {
  296.       printstat(argv[1], st);
  297.     } else {
  298.       perror(argv[1]);
  299.     }
  300.   } 
  301.  
  302.   argv++;
  303.   if (argc > 3) {
  304.     int fd = open(argv[1], O_RDONLY, 0);
  305.  
  306.     if (fd > 0) {
  307.       if (fstat(fd, st) == 0) {
  308.     printstat(argv[1], st);
  309.       } else {
  310.     perror("fstat");
  311.       }
  312.     } else {
  313.       perror(argv[1]);
  314.     }
  315.   }
  316. }
  317.  
  318. #endif
  319.  
  320.